#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>

#include "sqlite3.h"
#include "cgic.h"
#include "sem.h"
#include "head_2.h"

#define N	64

int cgiMain()
{
	sendEnv *shm_buf = NULL;
	
	struct _rt_info{
	unsigned char sto_status;
	unsigned char led_status;
	unsigned char beep_status;
	unsigned char fan_status;
	unsigned char seg_status;
	signed char x;
	signed char y;
	signed char z;
	float tem;
	float temMIN;
	float temMAX;
	float hum;
	float humMIN;
	float humMAX;
	float ill;
	float illMIN;
	float illMAX;
	float bat;
	float adc;
	};
	
	int shmid;
	int semid;
	key_t key;
	
	struct _rt_info rt_buf = 
	{
		1,1,1,1,1,
		1,2,3,
		50.6,10.0,50.0,
		9.3,10.0,50.0,
		300.6,10.0,500.0,
		3.40,2.30,
	};
	
	
	if((key = ftok("/app",'X')) < 0){
		perror("fail to ftok");
		exit(1);
	}
	
	shmid = shmget(key, sizeof(shm_buf), IPC_CREAT|0666);

	if(shmid < 0){
		perror("fail to shmget");
	}
	
	if((semid = semget(key, 1, 0666)) < 0){
		perror("semget");
		exit(1);
	}
	
	if((shm_buf = (sendEnv *)shmat(shmid,NULL,0)) == (void *)-1){
		perror("fail to shmat");
		exit(1);
	} 
	
	sem_p(semid,0);
	
//	shmctl(shmid, IPC_STAT,NULL);

	/*
	rt_buf.sto_status = shm_buf->rt_status.storage_no[STORAGE_NO].storage_status;
	rt_buf.led_status = shm_buf->rt_status.storage_no[STORAGE_NO].led_status;
	rt_buf.beep_status = shm_buf->rt_status.storage_no[STORAGE_NO].buzzer_status;
	rt_buf.fan_status = shm_buf->rt_status.storage_no[STORAGE_NO].fan_status;
	rt_buf.seg_status = shm_buf->rt_status.storage_no[STORAGE_NO].seg_status;
	rt_buf.x = shm_buf->rt_status.storage_no[STORAGE_NO].x;
	rt_buf.y = shm_buf->rt_status.storage_no[STORAGE_NO].y;
	rt_buf.z = shm_buf->rt_status.storage_no[STORAGE_NO].z;
	rt_buf.tem = shm_buf->rt_status.storage_no[STORAGE_NO].temperature;
	rt_buf.temMIN = shm_buf->rt_status.storage_no[STORAGE_NO].temperatureMIN;
	rt_buf.temMAX = shm_buf->rt_status.storage_no[STORAGE_NO].temperatureMAX;
	rt_buf.hum = shm_buf->rt_status.storage_no[STORAGE_NO].humidity;
	rt_buf.humMIN = shm_buf->rt_status.storage_no[STORAGE_NO].humidityMIN;
	rt_buf.humMAX = shm_buf->rt_status.storage_no[STORAGE_NO].humidityMAX;
	rt_buf.ill = shm_buf->rt_status.storage_no[STORAGE_NO].illumination;
	rt_buf.illMIN = shm_buf->rt_status.storage_no[STORAGE_NO].illuminationMIN;
	rt_buf.illMAX = shm_buf->rt_status.storage_no[STORAGE_NO].illuminationMAX;
	rt_buf.bat = shm_buf->rt_status.storage_no[STORAGE_NO].battery;
	rt_buf.adc = shm_buf->rt_status.storage_no[STORAGE_NO].adc;
	*/

/*	
	rt_buf.sto_status = shm_buf->rt_status.storage_no[STORAGE_NO].storage_status;
	rt_buf.led_status = shm_buf->rt_status.storage_no[STORAGE_NO].led_status;
	rt_buf.beep_status = shm_buf->rt_status.storage_no[STORAGE_NO].buzzer_status;
	rt_buf.fan_status = shm_buf->rt_status.storage_no[STORAGE_NO].fan_status;
	rt_buf.seg_status = shm_buf->rt_status.storage_no[STORAGE_NO].seg_status;
*/
	rt_buf.x = shm_buf->x;
	rt_buf.y = shm_buf->y;
	rt_buf.z = shm_buf->z;

	rt_buf.tem = shm_buf->temp[0];
//	rt_buf.temMIN = shm_buf->rt_status.storage_no[STORAGE_NO].temperatureMIN;
//	rt_buf.temMAX = shm_buf->rt_status.storage_no[STORAGE_NO].temperatureMAX;
	rt_buf.hum = shm_buf->hum[0];
//	rt_buf.humMIN = shm_buf->rt_status.storage_no[STORAGE_NO].humidityMIN;
//	rt_buf.humMAX = shm_buf->rt_status.storage_no[STORAGE_NO].humidityMAX;
	rt_buf.ill = shm_buf->ill;
//	rt_buf.illMIN = shm_buf->rt_status.storage_no[STORAGE_NO].illuminationMIN;
//	rt_buf.illMAX = shm_buf->ill;
	rt_buf.bat = shm_buf->bet;
	rt_buf.adc = shm_buf->adc;

	
	sem_v(semid,0);
	
//	while(1){	

	cgiHeaderContentType("text/html"); 
	fprintf(cgiOut, "<HTML><HEAD>"); 
	fprintf(cgiOut, "<TITLE>当前实时数据</TITLE>");  
	fprintf(cgiOut, "<style type=\"text/css\"> body,td,th {font-size: 12px;margin-left: 0px;margin-top: 0px;}</style></HEAD>"); 
	fprintf(cgiOut, "<BODY BGCOLOR=\"#F8F9FA\">"); 

	fprintf(cgiOut, "<meta http-equiv=\"refresh\" content=\"1\">");
	if (rt_buf.sto_status)
	{
		fprintf(cgiOut, "<table width=\"100%%\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\">");
		// 实时数据表头
		fprintf(cgiOut, 	"<tr bgcolor=\"#DEE4E6\">");
		
		fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">名称</td>");
		fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">最新数值</td>");
		fprintf(cgiOut, 	"</tr>");
		
		// 温度采集	
		fprintf(cgiOut, 	"<tr bgcolor=\"#f2f2f2\">");
		fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">环境温度</td>");
		fprintf(cgiOut, 	"<td height=\"30\" align=\"center\" valign=\"middle\">%0.1f℃</td>", rt_buf.tem);
		fprintf(cgiOut, 	"</tr>");
		
		// 湿度采集
		fprintf(cgiOut, 	"<tr>");
		fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">环境湿度</td>");
		fprintf(cgiOut, 	"<td height=\"30\" align=\"center\" valign=\"middle\">%0.1f％</td>", rt_buf.hum);
		fprintf(cgiOut, 	"</tr>");
		
		// 光照采集
		fprintf(cgiOut, 	"<tr bgcolor=\"#f2f2f2\">");
		fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">环境光照</td>");
		fprintf(cgiOut, 	"<td height=\"30\" align=\"center\" valign=\"middle\">%0.1flux</td>", rt_buf.ill);
		fprintf(cgiOut, 	"</tr>");
		
		// 电量采集
		fprintf(cgiOut, 	"<tr>");
		fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">电池电量</td>");
		fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">%0.2fV</td>", rt_buf.bat);
		fprintf(cgiOut, 	"</tr>");
		
		// ADC采集	
		fprintf(cgiOut, 	"<tr bgcolor=\"#f2f2f2\">");
		fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">模拟电压</td>");
		fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">%0.2fV</td>", rt_buf.adc);
		fprintf(cgiOut, 	"</tr>");
		
//		DealAxis3();
		fprintf(cgiOut, 	"<tr>");
		fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">设备姿态</td>");
		fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\"><table width=\"200\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">");
		fprintf(cgiOut, 			"<tr align=\"center\" valign=\"middle\">");
		fprintf(cgiOut, 					"<td width=\"25%%\">&nbsp;</td>");
		fprintf(cgiOut, 					"<td width=\"25%%\">X</td>");
		fprintf(cgiOut, 					"<td width=\"25%%\">Y</td>");
		fprintf(cgiOut, 					"<td width=\"25%%\">Z</td>");
		fprintf(cgiOut, 			"</tr>");
		fprintf(cgiOut, 			"<tr align=\"center\" valign=\"middle\">");
		fprintf(cgiOut, 					"<td>原始值</td>");
		fprintf(cgiOut, 					"<td>%d</td>", rt_buf.x);
		fprintf(cgiOut, 					"<td>%d</td>", rt_buf.y);
		fprintf(cgiOut, 					"<td>%d</td>", rt_buf.z);
		fprintf(cgiOut, 			"</tr>");

		/*
		fprintf(cgiOut, 			"<tr align=\"center\" valign=\"middle\">");
		fprintf(cgiOut, 					"<td>加速度</td>");
		fprintf(cgiOut, 					"<td>%0.1f</td>", Axis3.X.Acc);
		fprintf(cgiOut, 					"<td>%0.1f</td>", Axis3.Y.Acc);
		fprintf(cgiOut, 					"<td>%0.1f</td>", Axis3.Z.Acc);
		fprintf(cgiOut, 			"</tr>");
		fprintf(cgiOut, 			"<tr align=\"center\" valign=\"middle\">");
		fprintf(cgiOut, 					"<td>角度值</td>");
		fprintf(cgiOut, 					"<td>%0.1f</td>", Axis3.X.Angle);
		fprintf(cgiOut, 					"<td>%0.1f</td>", Axis3.Y.Angle);
		fprintf(cgiOut, 					"<td>%0.1f</td>", Axis3.Z.Angle);
		fprintf(cgiOut, 			"</tr>");
*/

		fprintf(cgiOut, 		"</table></td>");
		fprintf(cgiOut, 	"</tr>");
		fprintf(cgiOut, "</table>");
	}
	else
	{
		fprintf(cgiOut, "<H2>网络异常！</H2>");	
	}
	fprintf(cgiOut, "<script type=\"text/jscript\">");
	fprintf(cgiOut, "setTimeout(\"self.close()\", 3000)");
	fprintf(cgiOut, "</script>");
	fprintf(cgiOut, "</BODY>"); 
	fprintf(cgiOut, "</HTML>");
	fflush(stdout);
//	}
	
	return 0;
}





